<template>
  <div class="table-container">
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :stripe="true"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #judgeInfo="{ record }">
        <div>
          <p>Message: {{ record.judgeInfo?.message || "无" }}</p>
          <p>Memory: {{ record.judgeInfo?.memory || "无" }}</p>
          <p>Time: {{ record.judgeInfo?.time || "无" }}</p>
        </div>
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doQuestionPage(record)">
            开始
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import moment from "moment";
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionSubmitControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import { useRouter } from "vue-router";
import message from "@arco-design/web-vue/es/message";

const tableRef = ref();

const columns = [
  {
    title: "题号",
    dataIndex: "id",
    width: 200,
  },
  {
    title: "编程语言",
    dataIndex: "language",
    width: 100,
  },
  {
    title: "用户代码",
    dataIndex: "code",
    width: 500,
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
    width: 180,
  },
  {
    title: "题目 id",
    dataIndex: "questionId",
    width: 200,
  },
  {
    title: "创建时间",
    slotName: "createTime",
    width: 150,
  },
  {
    slotName: "optional",
  },
];

const dataList = ref([]);

const searchParams = ref<QuestionSubmitQueryRequest>({
  title: "",
  tags: [],
  pageSize: 5,
  current: 1,
});

const total = ref(0);

const router = useRouter();

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const doQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

const loadData = async () => {
  const res =
    await QuestionSubmitControllerService.listQuestionSubmitByPageUsingPost(
      searchParams.value
    );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

watchEffect(() => {
  loadData();
});
onMounted(() => {
  loadData();
});
</script>

<style scoped>
.table-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 使容器高度占满整个视口 */
}

.arco-table {
  width: 90%; /* 表格宽度设置为90%，并根据需要调整 */
  max-width: 1200px; /* 最大宽度限制 */
}

@media (max-width: 768px) {
  .arco-table {
    width: 100%; /* 在小屏幕上表格宽度占满整个容器 */
  }
}
</style>
